package com.hougong.lineclient.view;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hougong.lineclient.common.GlobalData;
import com.hougong.lineclient.entity.MyFriendVo;
import com.hougong.lineclient.entity.PersonMessageVo;
import com.hougong.lineclient.utils.DateUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class BootView {
    public static final int LineLen = 80;

    public static void bootMenu() {
        System.out.println("******** 菜单 **********");
        System.out.println("---  1.登录  ---");
        System.out.println("---  2.注册  ---");
        System.out.println("---  3.退出  ---");
        System.out.println("*********************");
    }

    public void inputBootMenu() {
        xinhaoFill("输入菜单id:(1,2,3):");
    }

    public void xinhaoFill(String content) {
        int len = content.length();
        StringBuilder sb = new StringBuilder();
        if (content == null) {
            System.out.println("****************************************");
        } else {
            if (len < LineLen) {
                int dif = LineLen - len;
                int difhalf = dif / 3;
                for (int i = 0; i < difhalf; i++) {
                    sb.append("*");
                }
                sb.append(" ").append(content).append(" ");
                for (int i = 0; i < difhalf; i++) {
                    sb.append("*");
                }
                System.out.println(sb.toString());
            } else {
                System.out.println("* " + content);
            }
        }
    }

    public void xinhaoFill() {
        xinhaoFill(null);
    }


    public void friendList(List<MyFriendVo> myFriendListVoList) {
        xinhaoFill("我的好友列表");
        for (MyFriendVo friendListVo : myFriendListVoList) {
            Integer unreadnum = friendListVo.getUnreadnum();
            System.out.println("id:" + friendListVo.getUid() + "--用户名<" + friendListVo.getUsername() + ">" + "--昵称<"
                    + friendListVo.getNickname() + ">" + "----未读消息<" + (unreadnum == null ? 0 : unreadnum) + ">条 "+(friendListVo.getOnlinestatus()==0?"离线":"在线"));
        }
    }

    public void groupListView(JSONArray jsonArray){
        xinhaoFill("群列表");
        for(int i=0;i<jsonArray.size();i++ ){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            System.out.println("群id:"+jsonObject.getLong("groupId")+" "+jsonObject.getString("groupRemark"));
        }
    }

    /**
     * 好友对话视图
     * @param friendUserName
     * @param personMessageVoList
     */
    public void friendChatView(String friendUserName, List<PersonMessageVo> personMessageVoList) {
        for (PersonMessageVo personMessageVo : personMessageVoList) {
            if (personMessageVo.getId() == GlobalData.uid) {
                System.out.println("我说 : "+personMessageVo.getContent()+"      " + DateUtils.formatDateTime(personMessageVo.getCreateTime()));
                System.out.println();
            } else {
                System.out.println(friendUserName + " : " + personMessageVo.getContent()+"      " + DateUtils.formatDateTime(personMessageVo.getCreateTime()));
                System.out.println();
            }
        }
    }

    public void groupChatView(JSONArray groupMessageList){
        for(int i=0;i<groupMessageList.size();i++){
            JSONObject jsonObject=groupMessageList.getJSONObject(i);
            System.out.println(jsonObject.getString("username")+" : "+jsonObject.getString("content")+"   -----"+jsonObject.getDate("crete_time"));
        }
    }


    public void applyFriendListView(JSONArray jsonArray){
        xinhaoFill("好友申请通知");
        for(int i=0;i<jsonArray.size();i++){
            JSONObject o = jsonArray.getJSONObject(i);
            Integer applytype = o.getInteger("applytype");
            Integer status = o.getInteger("status");
            String statusStr="";
            String date=DateUtils.formatDateTime(new Date(o.getLong("timestamp")));
            if(status==0){
                // -1被拒接  0等待中  1 接受  int
                statusStr="等待中";
            }
            if(status==-1){
                statusStr="已拒绝";
            }
            if(status==1){
                statusStr="已接受";
            }
            if(applytype==0){
                String opera="";
                if(status==0){
                    opera="输入命令accept "+o.getLong("applyaddfriendid")+" 接受"+",reject "+o.getLong("applyaddfriendid")+"拒绝";
                }
                System.out.println("用户("+o.getString("username")+")申请添加你为好友 --  状态:"+statusStr+" "+opera+"  "+date);
            }else {
                System.out.println("申请添加"+o.getString("username")+"为好友 --  状态:"+statusStr+"  "+date);
            }
        }
        xinhaoFill("输入_fuck返回");
    }
}
